package com.ymatou.query.seller.trading.api.parameter;

import java.util.ArrayList;
import java.util.List;

import com.ymttest.common.enums.OrderStatusEnum;
import com.ymttest.utils.DataUtil;

@Deprecated
public class GetSellerMainOrderListQuery {
	private Integer sellerId;
	private String addBeginTime;
	private String addEndTime;
	private String buyerNickName;
	private String paidBeginTime;
	private String paidEndTime;
	private Integer orderId;
	private String productName;
	private Integer mainOrderId;
	private Integer remarkLevel;
	private List<Integer> orderStatusList;
	private int pageNum;
	private int pageSize;
	private Boolean isPaidInFull;

	public Boolean getIsPaidInFull() {
		return isPaidInFull;
	}

	public void setIsPaidInFull(Boolean isPaidInFull) {
		this.isPaidInFull = isPaidInFull;
	}

	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getPageNum() {
		return this.pageNum;
	}

	public int getPageSize() {
		return this.pageSize;
	}

	public GetSellerMainOrderListQuery() {

	}

	public GetSellerMainOrderListQuery(int pageNum, int pageSize) {
		this.pageNum = pageNum;
		this.pageSize = pageSize;
	}

	public Integer getSellerId() {
		return sellerId;
	}

	public void setSellerId(Integer sellerId) {
		this.sellerId = sellerId;
	}

	public String getAddBeginTime() {
		return addBeginTime;
	}

	public void setAddBeginTime(String addBeginTime) {
		this.addBeginTime = addBeginTime;
	}

	public String getAddEndTime() {
		return addEndTime;
	}

	public void setAddEndTime(String addEndTime) {
		this.addEndTime = addEndTime;
	}

	public String getBuyerNickName() {
		return buyerNickName;
	}

	public void setBuyerNickName(String buyerNickName) {
		this.buyerNickName = buyerNickName;
	}

	public String getPaidBeginTime() {
		return paidBeginTime;
	}

	public void setPaidBeginTime(String paidBeginTime) {
		this.paidBeginTime = paidBeginTime;
	}

	public String getPaidEndTime() {
		return paidEndTime;
	}

	public void setPaidEndTime(String paidEndTime) {
		this.paidEndTime = paidEndTime;
	}

	public Integer getOrderId() {
		return orderId;
	}

	public void setOrderId(Integer orderId) {
		this.orderId = orderId;
	}

	public String getProductName() {
		return productName;
	}

	public void setProductName(String productName) {
		this.productName = productName;
	}

	public Integer getMainOrderId() {
		return mainOrderId;
	}

	public void setMainOrderId(Integer mainOrderId) {
		this.mainOrderId = mainOrderId;
	}

	public Integer getRemarkLevel() {
		return remarkLevel;
	}

	public void setRemarkLevel(Integer remarkLevel) {
		this.remarkLevel = remarkLevel;
	}

	public List<Integer> getOrderStatusList() {
		return orderStatusList;
	}

	public void setOrderStatusList(List<Integer> orderStatusList) {
		this.orderStatusList = orderStatusList;
	}

	private boolean isConditionEmpty(StringBuffer condition) {
		if (condition.length() == 0) {
			return true;
		}
		return false;
	}

	private void addAnd(StringBuffer condition) {
		if (isConditionEmpty(condition) == false) {
			condition.append(" and ");
		}
	}

	public String toString() {
		StringBuffer condition = new StringBuffer();
		if (sellerId != null) {
			addAnd(condition);
			condition.append(" o.iBuyerId = " + sellerId);
		}
		if (addBeginTime != null) {
			addAnd(condition);
			condition.append(" o.dAddTime >= '" + addBeginTime + " 00:00:00'");
		}
		if (addEndTime != null) {
			addAnd(condition);
			condition.append(" o.dAddTime <= '" + addEndTime + " 23:59:59'");
		}
		if (buyerNickName != null) {
			addAnd(condition);
			condition.append(" o.sBuyerNickName = '" + buyerNickName + "'");
		}
		if (paidBeginTime != null) {
			addAnd(condition);
			condition.append(" (o.dPostPaidTime is null and o.dPaidTime >= '"
					+ paidBeginTime + " 00:00:00' or o.dPostPaidTime >= '"
					+ paidBeginTime + " 00:00:00')");
		}
		if (paidEndTime != null) {
			addAnd(condition);
			condition.append(" (o.dPostPaidTime is null and o.dPaidTime <= '"
					+ paidEndTime + " 23:59:59' or o.dPostPaidTime <= '"
					+ paidEndTime + " 23:59:59')");
		}
		if (orderId != null) {
			addAnd(condition);
			condition.append(" o.iOrderId = " + orderId);
		}
		if (productName != null) {
			addAnd(condition);
			condition.append(" oi.sTitle like '%" + productName + "%'");
		}
		if (mainOrderId != null) {
			addAnd(condition);
			condition.append(" o.iMainOrderId = " + mainOrderId);
		}
		int bPaidInFull = 0;
		if (isPaidInFull != null) {
			if (isPaidInFull) {
				bPaidInFull = 1;
			}
			addAnd(condition);
			condition.append(" o.bPaidInFull = " + bPaidInFull);
		}
		if (remarkLevel != null) {
			addAnd(condition);
			condition.append(" note.iRemarkLevel = " + remarkLevel);
		}
		StringBuffer sb = new StringBuffer();
		List<Integer> tempOrderStatusList = new ArrayList<Integer>();
		if (!DataUtil.isNullOrEmpty(orderStatusList)) {
			tempOrderStatusList.addAll(orderStatusList);
		}
		if (tempOrderStatusList.contains(OrderStatusEnum.SellerAccept
				.getValue())) {
			tempOrderStatusList.removeIf(a -> a == OrderStatusEnum.SellerAccept
					.getValue());
			sb.append(" (o.iTradingStatus = 17 and o.bPaidInFull = 1)");
		}
		if (tempOrderStatusList.contains(OrderStatusEnum.OrderPostPay
				.getValue())) {
			tempOrderStatusList.removeIf(a -> a == OrderStatusEnum.OrderPostPay
					.getValue());
			if (sb.length() > 1) {
				sb.append(" or ");
			}
			sb.append(" (o.iTradingStatus = 17 and o.bPaidInFull = 0)");
		}
		if (!DataUtil.isNullOrEmpty(tempOrderStatusList)) {
			if (sb.length() > 1) {
				sb.append(" or ");
			}
			sb.append(" o.iTradingStatus in "
					+ DataUtil.toInOfSql(tempOrderStatusList));
		}
		if (!DataUtil.isNullOrEmpty(orderStatusList)) {
			condition.append(" and (" + sb.toString() + ")");
		}

		condition.append(" order by o.iMainOrderId desc, o.iOrderId asc ");

		if (pageNum > 0 && pageSize > 0) {
			condition.append(" offset ");
			condition.append((pageNum - 1) * pageSize);
			condition.append(" rows fetch next  ");
			condition.append(pageSize);
			condition.append("  rows only  ");
		}
		return condition.toString();
	}
}